<?php

class Email {

	private $subject;

	private $body;

	private $HTML_message;

	private $text_message;

	private $mime_boundary;

	private $headers;

	public function __construct($from, $from_name, $system_name){
		$elements = explode("@", $from);
		if(2==sizeof($elements)) $domain = $elements[1];
		else throw new Exception("Email $from is not a valid address");
		$this->mime_boundary = "==MULTIPART_BOUNDARY_".md5(time());
		// Headers
		$this->headers = "MIME-Version: 1.0\n";
		$this->headers.= "Content-type: multipart/alternative;\n     boundary=\"".$this->mime_boundary."\"\n";
		$this->headers.= "Organization: ".$system_name."\n";
		$this->headers.= "From: $from_name <$from>\n";
		$this->headers.= "Reply-To: $from\n";
		$this->headers.= "Message-ID: <".md5(uniqid(time()))."@$domain>\n";
		$this->headers.= "Return-Path: $from\n";
		$this->headers.= "X-Priority: 3\n";
		$this->headers.= "X-MSmail-Priority: Normal\n";
		$this->headers.= "X-Mailer: ".$system_name." Mailer V1.1\n";
		$this->headers.= "X-MimeOLE: Produced By ".$system_name." MimeOLE V1.1\n";
		$this->headers.= "X-Sender: $from\n";
	}

	public function set_message($subject, $HTML_message){
		$this->subject = $subject;
		$this->HTML_message = $HTML_message;
		$this->text_message = strip_tags(str_replace("<hr/>", "-----------------------------------------------------------", $HTML_message));
		$this->body = "This is a multi-part message in MIME format.\n";
		$this->body.= "\n";
		$this->body.= "--".$this->mime_boundary."\n";
		$this->body.= "Content-Type: text/plain; charset=utf-8\n";
		$this->body.= "Content-Transfer-Encoding: 7bit\n";
		$this->body.= "\n";
		$this->body.= $this->text_message."\n";
		$this->body.= "\n";
		$this->body.= "--".$this->mime_boundary."\n";
		$this->body.= "Content-Type: text/html; charset=utf-8\n";
		$this->body.= "Content-Transfer-Encoding: 7bit\n";
		$this->body.= "\n";
		$this->body.= "$HTML_message\n";
		$this->body.= "\n";
		$this->body.= "--".$this->mime_boundary."--\n";
	}

	public function get_message(){
		return $this->HTML_message;
	}

	public function send($to,$extra_param = ''){
		return @mail($to, $this->subject, $this->body, $this->headers,$extra_param);
	}


} // End Email